//
//  UIImage+WLLoadImageAfter.m
//  CeXi
//
//  Created by 王亮 on 2017/5/24.
//  Copyright © 2017年 wangLiang. All rights reserved.
//

#import "UIImage+WLLoadImageAfter.h"

@implementation UIImage (WLLoadImageAfter)


-(UIImage *)loadImageAfter{
    UIImage *image = [self copy];
    // 设置上左下右边距
    CGFloat topMode= image.size.height * 0.5f;
    CGFloat leftMode= image.size.width * 0.5f;
    CGFloat bottomMode= image.size.height * 0.5f;
    CGFloat rightMode= image.size.width * 0.5f;
    
    UIEdgeInsets edgeInsetsMode= UIEdgeInsetsMake(topMode, leftMode, bottomMode, rightMode);
    // 拉伸图片
    UIImage *edgeModeImage = [image resizableImageWithCapInsets:edgeInsetsMode resizingMode:UIImageResizingModeStretch];
    
    edgeModeImage = [edgeModeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    return edgeModeImage;
}
-(UIImage *)loadImageAfterEdge:(UIEdgeInsets)edgeInsets{
    UIImage *image = [self copy];
    // 设置上左下右边距
    CGFloat topMode= image.size.height * edgeInsets.top;
    CGFloat leftMode= image.size.width * edgeInsets.left;
    CGFloat bottomMode= image.size.height * edgeInsets.bottom;
    CGFloat rightMode= image.size.width * edgeInsets.right;

    UIEdgeInsets edgeInsetsMode= UIEdgeInsetsMake(topMode, leftMode, bottomMode, rightMode);
    // 拉伸图片
    UIImage *edgeModeImage = [image resizableImageWithCapInsets:edgeInsetsMode resizingMode:UIImageResizingModeStretch];
    edgeModeImage = [edgeModeImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    return edgeModeImage;
}

-(UIImage *)loadImageAfterWidth:(CGFloat)defineWidth{
    UIImage *sourceImage = [self copy];
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = height / (width / targetWidth);
    CGSize size = CGSizeMake(targetWidth, targetHeight);
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    if(CGSizeEqualToSize(imageSize, size) ==NO){
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if(widthFactor > heightFactor){
            scaleFactor = widthFactor;
        }else{
            scaleFactor = heightFactor;
        }
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        if(widthFactor > heightFactor){
            thumbnailPoint.y = (targetHeight - scaledHeight) *0.5;
        }else if(widthFactor < heightFactor){
            thumbnailPoint.x = (targetWidth - scaledWidth) *0.5;
        }
    }
    UIGraphicsBeginImageContext(size);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil){
        NSLog(@"scale image fail");
    }
    UIGraphicsEndImageContext();
    return newImage.loadImageAfter;
}
-(UIImage *)loadImageAfterHeight:(CGFloat)defineHeight{
    UIImage *sourceImage = [self copy];
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = width / (height / defineHeight);
    CGFloat targetHeight = defineHeight;
    CGSize size = CGSizeMake(targetWidth, targetHeight);
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
    if(CGSizeEqualToSize(imageSize, size) ==NO){
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        if(widthFactor > heightFactor){
            scaleFactor = widthFactor;
        }else{
            scaleFactor = heightFactor;
        }
        scaledWidth = width * scaleFactor;
        scaledHeight = height * scaleFactor;
        if(widthFactor > heightFactor){
            thumbnailPoint.y = (targetHeight - scaledHeight) *0.5;
        }else if(widthFactor < heightFactor){
            thumbnailPoint.x = (targetWidth - scaledWidth) *0.5;
        }
    }
    UIGraphicsBeginImageContext(size);
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width = scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    [sourceImage drawInRect:thumbnailRect];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil){
        NSLog(@"scale image fail");
    }
    UIGraphicsEndImageContext();
    return newImage.loadImageAfter;
}

//图片转字符串
-(NSString *)toString_wl
{
    NSData *data = UIImageJPEGRepresentation(self, 1.0f);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return encodedImageStr;
}
@end
